<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        .active {
            color: green;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="cname = 'one' " :class="{ active: cname == 'one' }">one</button>
        <button @click="cname = 'two' " :class="{ active: cname == 'two' }">two</button>
        <button @click="cname = 'three' " :class="{ active: cname == 'three' }">three</button>
        <div>
            <!-- <one></one> -->
            <keep-alive>
                <component :is="cname"></component>
            </keep-alive>

        </div>


    </div>
    <script>
        Vue.component("one", {
            template: `
                <div>
                   one 的内容 {{ username }}
                </div>
            `,
            data() {
                return {
                    username: ""
                }
            },
            created() {
                fetch("./1.json")
                    .then(res => res.json())
                    .then(res => {
                        console.log(res);
                        this.username = res.name
                    })
            }
        })

        Vue.component("two", {
            template: `
                <div>
                   two 组件内容
                </div>
            `,
        })

        Vue.component("three", {
            template: `
                <div>
                   three 组件
                </div>
            `,
        })

        new Vue({
            el: "#app",
            data() {
                return {
                    cname: "one"
                }
            }


        })
    </script>
</body>

</html>